package com.hongxun.budget.ui.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hongxun.budget.R;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.builder.FooterBuilder;
import com.jessewu.library.paging.LoadDataListener;
import com.jessewu.library.paging.LoadDataStatus;
import com.jessewu.library.widget.SimpleFooterBuilder;

import butterknife.BindView;

/**
 * Created by admin on 2018/5/15.
 */

public abstract class BaseListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.list)
    public RecyclerView list;
    @BindView(R.id.refresh)
    public SwipeRefreshLayout refresh;
    public LoadDataStatus loadDataStatus;//加载更多。。。
    public int page = 1;
    public int limit = 15;

    @Override
    public  int getLayoutId() {
        return getMyListLayoutId() == 0 ? R.layout.activity_base_list : getMyListLayoutId();
    }

    @Override
    public  void initView(Bundle savedInstanceState) {
        refresh.setEnabled(false);
        refresh.setColorSchemeColors(new int[]{R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark});
        refresh.setOnRefreshListener(this);
        list.setLayoutManager(getLayoutManager() == null ? new LinearLayoutManager(getApplicationContext()) : getLayoutManager());
        list.setAdapter(getAdapter());
    }

    public  int getMyListLayoutId() {
        return 0;
    }


    @Override
    public void onRefresh() {

    }

    public RecyclerView.LayoutManager getLayoutManager() {
        return null;
    }

    public abstract SuperAdapter getAdapter();

    public FooterBuilder getLoadMoreFooter() {
        return new SimpleFooterBuilder("这是个底部", "正在加载数据中", "加载数据失败", "已经到底啦");
    }

    public LoadDataListener getLoadMoreListener() {
        return new LoadDataListener() {
            @Override
            public void onLoadingData(int loadPage, LoadDataStatus loadDataStatus) {
                loadMore(loadPage, loadDataStatus);
            }
        };
    }

    /**
     * 设置上拉刷新
     *
     * @param adapter
     */
    public void setLoadMore(SuperAdapter adapter) {
        adapter.addFooter(getLoadMoreFooter());
        adapter.setPaginationData(-1, getLoadMoreListener());
    }

    public void loadMore(int loadPage, LoadDataStatus loadDataStatus) {
        this.loadDataStatus = loadDataStatus;
    }

}
